package com.bw.counter.view

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.constraintlayout.widget.ConstraintLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.counter.R
import com.bw.counter.databinding.ActivityAddCountBinding
import com.bw.counter.entity.CounterData
import com.bw.counter.net.CounterViewModel
import java.text.SimpleDateFormat
import java.util.Calendar

/**
 *  智慧园区-05-新增临时收费 -v1.1-张梦停
 *  2025/05/09
 */
@Route(path = RoutePath.PATH_ADD_Count)
class AddCountActivity : BaseMvvmActivity<ActivityAddCountBinding,CounterViewModel>(){
    override fun initView() {
        binding.addCountBar.ivBack.setOnClickListener { finish() }
        binding.addCountBar.ivMore.visibility = View.VISIBLE
        binding.addCountBar.tvTitle.text = "新增临时收费"
        val options = listOf("请选择","智慧园区物业费","智慧园区停车费",)
        val adapter = ArrayAdapter(this,R.layout.custom_spinner_item,options)
         //     R.layout.custom_spinner_item   根视图是TextView
        binding.spinner.adapter = adapter
        binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                val selectItem = p0?.getItemAtPosition(p2).toString()
                    binding.et1.text = Editable.Factory.getInstance().newEditable(selectItem)
                binding.et1.visibility = View.GONE

            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }
        val calendar = Calendar.getInstance()
        // 时间选择器
        binding.time1.setOnClickListener {
            val dateDia = DatePickerDialog(this,DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                val selectedDate = "$year-${(month+1)}-$dayOfMonth"
                binding.et4.text = Editable.Factory.getInstance().newEditable(selectedDate)
            },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH))
            dateDia.show()
        }
        binding.time2.setOnClickListener {
            val dateDia = DatePickerDialog(this,DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                val selectedDate = "$year-${(month+1)}-$dayOfMonth"
                binding.et5.text = Editable.Factory.getInstance().newEditable(selectedDate)
            },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH))
            dateDia.show()
        }
    }

    @SuppressLint("SimpleDateFormat")
    override fun initData() {
        var id = 0
        binding.bu.setOnClickListener {
            val time1 = System.currentTimeMillis()
            val format1 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val s = format1.format(time1)
            vm.addCount(CounterData(
                "19371032626","19320203333",
                id++ ,1, "王五",
                "503",binding.et2.text.toString(),
                "已缴费",binding.et5.text.toString() ,
                "王五",binding.et3.text.toString().toInt(),
                "AA20250306001"
            ))
        }
            // et5 是房间号

        vm.addCountSuccess.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

}